package com.card.match;

import java.util.List;

import com.card.common.CardContants;
import com.google.common.collect.Lists;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.Key;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PrimaryKey;

public class MatchSetting implements Comparable<MatchSetting> {
	private int id;
	private int gameId;
	private String name;
	private int num;
	private int enterDiamond;
	private int state;

	public static final String ID = "id";
	public static final String GameId = "GameId";
	public static final String State = "State";
	public static final String Name = "name";
	public static final String Num = "num";
	public static final String Reward = "reward";
	public static final String EnterDiamond = "enterDiamond";

	public MatchSetting(int gameId, String name, int num, int enterDiamond) {
		this.id = PrimaryKey.getIntPrimaryKey("MatchData");
		this.gameId = gameId;
		this.name = name;
		this.num = num;
		this.enterDiamond = enterDiamond;
		save();
	}

	public MatchSetting(Entity entity) {
		this.id = (int) entity.getProperty(ID);
		this.gameId = (int) entity.getProperty(GameId);
		this.state = (int) entity.getProperty(State);
		this.name = (String) entity.getProperty(Name);
		this.num = (int) entity.getProperty(Num);
		// this.reward = (int[])entity.getProperty(Reward);
		this.enterDiamond = (int) entity.getProperty(EnterDiamond);
	}

	public void save() {
		Entity entity = new Entity(new Key(MatchManager.T_Table, id + ""));
		entity.setProperty(ID, id);
		entity.setProperty(GameId, gameId);
		entity.setProperty(State, state);
		entity.setProperty(Name, name);
		entity.setProperty(Num, num);
		entity.setProperty(EnterDiamond, enterDiamond);
		MongodbManager.getInstance().saveOrUpdate(entity);
	}

	// public boolean isCanEnroll() {
	// return date.getTime() - System.currentTimeMillis()<GameConstants.MINUTE;
	// }
	//
	// public int getLastSecond() {
	// if(isCanEnroll()) {
	// return (int)((date.getTime() -
	// System.currentTimeMillis())/GameConstants.SECOND);
	// }
	// return -1;
	// }

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getGameName() {

		if (gameId == CardContants.GAMETYPE_SH) {
			return "麻将";
		} else if (gameId == CardContants.GAMETYPE_Ddz) {
			return "斗地主";
		} else if (gameId == CardContants.GAMETYPE_TGG) {
			return "炸金花";
		} else if (gameId == CardContants.GAMETYPE_Pdk) {
			return "跑的快";
		}else {
			return "牛牛";
		}
	}

	public int getNum() {
		return num;
	}

	public int[] getReward() {
		if (gameId == CardContants.GAMETYPE_SH) {
			// 1 2-3 4-8
			int[] mj = { enterDiamond * 6, enterDiamond * 2, enterDiamond };
			return mj;

		} else if (gameId == CardContants.GAMETYPE_Nn) {
			// 1 2 3
			int[] nn = { enterDiamond * 5, enterDiamond * 2, enterDiamond };
			return nn;	
		}else if(gameId == CardContants.GAMETYPE_TGG){
				// 1 2 3
				int[] zjh = {enterDiamond*5,enterDiamond*2,enterDiamond};
				return zjh;
		} else if(gameId == CardContants.GAMETYPE_Ddz) {
			// 1 2 3
			int[] ddz = { enterDiamond * 5, enterDiamond * 2, enterDiamond };
			return ddz;
		}else{
			// 1 2 3
			int[] pdk = {enterDiamond*5,enterDiamond*2,enterDiamond};
			return pdk;
		}
	}

	public int getGameId() {
		return gameId;
	}

	public int getEnterDiamond() {
		return enterDiamond;
	}

	public String getState() {
		return state == 0 ? "正常运行" : "已关闭";
	}

	public int getIntState() {
		return state;
	}

	public void changeState() {
		if (state == 0) {
			state = 1;
		} else {
			state = 0;
		}
	}

	public boolean isOpen() {
		return state == 0;
	}

	public static List<MatchSetting> getMatchSettings() {
		Query query = new Query(MatchManager.T_Table);
		query.setLimit(100);
		List<Entity> enls = MongodbManager.getInstance().queryEntity(query);
		List<MatchSetting> msl = Lists.newArrayList();
		for (Entity en : enls) {
			msl.add(new MatchSetting(en));
		}
		return msl;
	}

	@Override
	public int compareTo(MatchSetting o) {
		if (this.gameId == o.gameId) {
			return o.enterDiamond - enterDiamond;
		}
		return this.gameId - o.gameId;
	}
}
